<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-calendar"></i> 权限管理 </el-breadcrumb-item>
                <el-breadcrumb-item>权限修改</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="form" :model="form" label-width="80px" :rules="rules">
                    

                    <el-form-item label="权限名称" prop="name">
                        <el-input v-model="form.name" ></el-input>
                    </el-form-item>

                    <el-form-item label="权限类型" prop="type">
                       <el-select v-model="form.type" placeholder="类型" >
                            <el-option v-for="(item, index) in types" :key="index" :label="item.name" :value="item.code"> </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="状态">
                        <el-switch v-model="form.status" :active-value="'01'" :inactive-value="'00'" ></el-switch>
                    </el-form-item>


                    <el-form-item label="权限描述" prop="description">
                        <el-input type="textarea" rows="5" v-model="form.description" ></el-input>
                    </el-form-item>
                     <el-form-item>
                        <el-button @click="goback" icon="el-icon-arrow-left">返回</el-button>
                        <el-button type="primary" @click="onSubmit('form')" icon="el-icon-edit">修改权限</el-button>
                        <el-button type="warning" @click="reset('form')" icon="el-icon-refresh-right">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'baseform',
    inject: ['reload'],
    data() {
        return {
            id: this.$route.query.id,
            permission:{},
            types:[],
            form: {
                name: '',
                type:'',
                status: '',
                description: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入权限名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                type: [{ required: true, message: '请选择权限类型', trigger: 'blur' }]
            }
        };
    },
    created: function () {
        this.getType();
        this.init();
    },
    methods: {
        async init() {
            var a = await this.$http.get('/permission/' + this.id);
            if (a.code == 200) {
                this.permission = a.data;
                this.initForm(this.permission)
            } else {
                this.$message.error(a.message);
            }
        },
        goback(){
            this.$router.go(-1);
        },
        getType() {
            this.types = JSON.parse(window.sessionStorage.getItem('type'));
        },
        
         initForm(permission){
            this.form.id=this.id
            this.form.name=permission.name
            this.form.type=permission.type
            this.form.status=permission.status
            this.form.description=permission.description
            
        },
        
        onSubmit(formName) {
            this.form.id = this.id;
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    var d = await this.$http.put('/permission', this.form);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.init;
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                    this.$message.warning('请把内容填写完整!');
                }
            });
        },

        reset(formName) {
            this.$confirm('此操作将重置所有信息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.initForm(this.permission);
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消重置'
                    });
                });
        }

    }
};
</script>